<template>
  <Modal class="import-template" v-model="modalView" width="200" footer-hide :mask-closable="false">
    <p slot="header">
      <Icon type="ios-information-circle"></Icon>
      <span>导入设置</span>
    </p>
    <div class="mt info">
      <Button  class="mt" type="primary" size="large" @click="onDowload">下载模板</Button>
      <Upload
        accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel"
        :on-success="uploadSuccess"
        :on-error="uploadError"
        :data="paramData"
        :on-progress="uploaded"
        :show-upload-list="false"
        :headers="headers"
        name="file"
        action="/api/taiyi/factory/import/sku/batch">
          <Button class="mt" type="primary" size="large" @click="onAgree">导入商品</Button>
      </Upload>
    </div>
    <Loader :status="status"></Loader>
  </Modal>
</template>
<script>
import {staticPath} from '@/util'
export default {
  data() {
    return {
      timer: null,
      id: this.$route.query.id || '',
      paramData: {},
      type: '1',
      modalView: false,
      status: 'pass',
      headers: {
        Authorization: 'Bearer ' + this.$store.state.user.user.userToken
      }
    }
  },
  methods: {
    onDowload() {
      if (window.location.host.substring(9) === 'chaomeifan.com') { //如果是测试环境
        window.location.href = 'https://misc.hzzcckj.cn/skin/icon/promotionecel.xlsx'
      } else {
        window.location.href = staticPath + "/taiyi/template/%E6%B4%BB%E5%8A%A8%E5%95%86%E5%93%81.xlsx?v=1"
      }
    },
    open(type) {
      this.type = type
      this.modalView = true
      this.status = 'pass'
      this.timer = null
      this.paramData = {
        activityId: this.id,
        isLimitBrand: this.type === 1 ? false : true
      }
    },
    beforeUpload(file) {
      const isExcel = file.type.indexOf('sheet') !== -1
      if (!isExcel) {
        this.$Message.error('请上传Excel格式的文件')
      }
      return isExcel
    },
    uploaded(event, file, fileList) {
      this.status = 'loading'
    },
    uploadSuccess(res, file, fileList) {
      if (res.code !== 200) {
        this.$Notice.warning({
          title: '温馨提示',
          desc: res.message
        })
        this.status = 'pass'
        return
      }
      let param = {
        batchImportKey: res.data
      }
      this.comeAgain(param)
      // this.$emit('completeUpload', res.data, this.type)
      // this.modalView = false
    },
    comeAgain(param) { // 查询批量导入商品的返回结果(需要轮询)
      this.timer && clearTimeout(this.timer)
      this.$ajaxGet('/factory/import/sku/find_batchImportKey', param).then(res => {
        if (res) { // 轮询返回的数据
          this.$emit('completeUpload', res, this.type)
          this.modalView = false
        } else {
          this.timer = setTimeout(() => {
            this.comeAgain(param)
          }, 1000)
        }
      }).catch(err => {
        this.status = 'pass'
      })
    },
    uploadError() {
      this.status = 'pass'
      this.$Notice.error({
        title: "导入失败"
      })
    }
  }
}
</script>

